<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="success"
                    @click="hAssign(scope.row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="hEdit(scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page"
                :page-sizes="[1, 2, 3, 20]"
                :page-size="pagesize"
                layout="total, sizes, prev, pager, next"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ? '编辑弹层' : '添加弹层'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button
              size="small"
              @click="showDialog = falseF"
            >取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="hSubmit"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 分配权限开关 -->
      <el-dialog
        title="分配权限"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialogAssign"
      >
        <assign-permission
          v-if="showDialogAssign"
          :id="roleId"
          @close="showDialogAssign = false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, deleteRole, addRole, updateRole } from '@/api/setting'
import assignPermission from './assignPermission.vue'
export default {
  components: { assignPermission },
  data() {
    return {
      roles: [], // 保存所有角色列表
      total: 0, // 总条数
      // 分页数据
      page: 1, // 默认页码
      pagesize: 2, // 每页的数据
      showDialog: false, // 弹框开关
      // 弹框数据
      roleForm: {
        name: '',
        description: ''
      },
      // 弹框校验规则
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      },
      // 判断是否编辑
      isEdit: false,
      showDialogAssign: false, // 分配权限开关
      roleId: ''
    }
  },
  created() {
    // 获取所有角色列表
    this.loadRoles()
  },
  methods: {
    // 点击分配权限
    hAssign(id) {
      this.roleId = id
      this.showDialogAssign = true
    },
    // 获取所有角色列表
    async loadRoles() {
      try {
        const { data: res } = await getRoles({
          page: this.page,
          pagesize: this.pagesize
        })
        // console.log(res)
        this.roles = res.rows
        this.total = res.total
      } catch (error) {
        console.dir(error)
      }
    },
    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      // 重新赋值
      this.pagesize = val
      // 默认第一页
      this.page = 1
      // 重新获取
      this.loadRoles()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      // 重新赋值
      this.page = val
      // 重新获取
      this.loadRoles()
    },
    // 点击删除
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 确认
          this.doDel(id)
        })
        .catch(() => {
          // 取消
        })
    },
    // 删除申请
    async doDel(id) {
      try {
        await deleteRole(id)
        // 提示
        this.$message.success('删除成功')
        if (this.roles.length === 1 && this.page > 1) {
          this.page--
        }
        // 重新渲染
        this.loadRoles()
      } catch (error) {
        console.dir(error)
      }
    },
    // 点击确认
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    // 添加角色请求
    async doAdd() {
      try {
        // 1、发送请求
        await addRole(this.roleForm)
        // 2、关闭弹框
        this.showDialog = false
        // 3、重新渲染
        this.total++
        this.page = Math.ceil(this.total / this.pagesize)
        this.loadRoles()
      } catch (error) {
        console.dir(error)
      }
    },
    // 点击编辑
    hEdit(row) {
      // 1、显示弹框
      this.showDialog = true
      // 2、赋值
      this.roleForm = { ...row }
      // 3、判断
      this.isEdit = true
    },
    // 编辑角色请求
    async doEdit() {
      try {
        // 1、请求
        await updateRole(this.roleForm)
        // 2、关闭弹框
        this.showDialog = false
        // 3、重新渲染
        this.loadRoles()
      } catch (error) {
        console.dir(error)
      }
    },
    // 点击新增角色
    hAdd() {
      // 开启弹框
      this.showDialog = true
      // 判断
      this.isEdit = false
    },
    // 重置
    hClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    }
  }
}
</script>
